package dtaskboard.client.project.widget;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.VerticalPanel;

import dtaskboard.client.Constants;
import dtaskboard.client.DialogUtil;
import dtaskboard.client.dto.TaskDTO;
import dtaskboard.client.dto.TaskStatus;
import dtaskboard.client.project.ProjectPage;
import dtaskboard.client.service.ProjectService;
import dtaskboard.client.service.ProjectServiceAsync;

public class TaskWidget extends VerticalPanel {
  private final String projectName;
  private final Long taskId;

  public TaskWidget(String projectName, TaskDTO task) {
    this.projectName = projectName;
    this.taskId = task.getTaskId();

    this.add(new Label(task.getName()));
    this.addStyleName("task");

    Panel buttonPanel = new HorizontalPanel();

    if (task.getStatus() != TaskStatus.TODO) {
      buttonPanel.add(new ReturnTaskButton());
    }

    if (task.getStatus() != TaskStatus.DONE) {
      buttonPanel.add(new AdvanceTaskButton());
    }
    this.add(buttonPanel);
  }

  class ReturnTaskButton extends Button {
    private ReturnTaskButton() {
      this.setText("<");
      this.addClickHandler(new ClickHandler() {

        @Override
        public void onClick(ClickEvent event) {
          ProjectServiceAsync projectService = GWT.create(ProjectService.class);
          String sessionId = Cookies.getCookie(Constants.sessionIdCookie);
          final TaskWidget w = TaskWidget.this;
          projectService.returnTask(w.projectName, w.taskId, sessionId,
              new AsyncCallback<Void>() {
                @Override
                public void onFailure(Throwable caught) {
                  DialogUtil.closableDialog("Retornando tarefa",
                      "Ocorreu um erro ao tentar retornar a tarefa.");
                }

                @Override
                public void onSuccess(Void result) {
                  new ProjectPage(w.projectName);
                }
              });
        }
      });
    }
  }


  class AdvanceTaskButton extends Button {
    private AdvanceTaskButton() {
      this.setText(">");
      this.addClickHandler(new ClickHandler() {

        @Override
        public void onClick(ClickEvent event) {
          ProjectServiceAsync projectService = GWT.create(ProjectService.class);
          String sessionId = Cookies.getCookie(Constants.sessionIdCookie);
          final TaskWidget w = TaskWidget.this;
          projectService.advanceTask(w.projectName, w.taskId, sessionId,
              new AsyncCallback<Void>() {
                @Override
                public void onFailure(Throwable caught) {
                  DialogUtil.closableDialog("Avançando tarefa",
                      "Ocorreu um erro ao tentar avançar a tarefa.");
                }

                @Override
                public void onSuccess(Void result) {
                  new ProjectPage(w.projectName);
                }
              });
        }
      });
    }
  }
}
